package com.cwj.mansysback.dao.impl;

import com.cwj.mansysback.dao.BaseDao;
import com.cwj.mansysback.dao.api.RanksDao;
import com.cwj.mansysback.entity.Rank;
import com.cwj.mansysback.service.api.RankService;

import java.util.List;

public class RanksDaoImpl extends BaseDao<Rank> implements RanksDao {

    private static final String TABLE_NAME = "ranks";

    @Override
    public List<Rank> selectALL() {

//        String sql = "select rid,rname,rpiece from "+ RanksDaoImpl.TABLE_NAME+" order by rid ";

        String sql = "SELECT rk.*,t.* FROM `ranks` AS rk LEFT OUTER JOIN (\n" +
                "\tSELECT rid ,COUNT(1) AS teaCount,AVG(tsalary) AS avgSalary FROM teacher GROUP BY rid) \n" +
                "\tAS t ON rk.rid = t.rid";

        System.out.println(sql);

        return super.selectBatch(Rank.class,sql);
    }

    @Override
    public boolean addRank(String rid, String rname, Double rpiece) {

        String sql = "insert into "+ RanksDaoImpl.TABLE_NAME+"(rid,rname,rpiece) values(?,?,?)";

        return super.update(sql,rid,rname,rpiece) > 0 ;
    }

    @Override
    public Rank selectRank(String rid) {
        String sql = "select rid,rname,rpiece from "+ RanksDaoImpl.TABLE_NAME+" where rid = ?";

        return super.select(Rank.class,sql,rid);
    }

    @Override
    public boolean updateRank(String rid, String rname, Double rpiece) {
        String sql = "update "+ RanksDaoImpl.TABLE_NAME+" set rname = ? , rpiece = ? where rid =?";

        return super.update(sql,rname,rpiece,rid) > 0;
    }

    @Override
    public boolean deleteRank(String rid) {
        String sql = "delete from "+ RanksDaoImpl.TABLE_NAME +"\twhere rid = ?";
        return super.update(sql,rid) > 0;
    }
}
